package com.example.demoyyy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demoyyy.JieKouService.CategoryServiceJieKou;
import com.example.demoyyy.JieKouService.DishServiceJieKou;
import com.example.demoyyy.JieKouService.SetmealDishServiceJieKou;
import com.example.demoyyy.JieKouService.SetmealServiceJieKou;
import com.example.demoyyy.ShiTiYYY.Category;
import com.example.demoyyy.ShiTiYYY.Dish;
import com.example.demoyyy.ShiTiYYY.Setmeal;
import com.example.demoyyy.common.R;
import com.example.demoyyy.dto.SetmealDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealServiceJieKou setmealservicejiekou;
    @Autowired
    private CategoryServiceJieKou fuwufenleijiekou ;
    // 装配 分类服务层接口
    @Autowired
    private SetmealDishServiceJieKou setmealdishservicejiekou;
    @Autowired
    private DishServiceJieKou fuwucaipinjiekou;
    // 装配菜品服务层接口
    @PostMapping("")
    public R<Object> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);
        setmealservicejiekou.saveWithDish(setmealDto);
        return R.success(1,"新增套餐成功",null);
    }
    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Object> page(int page,int pageSize,String name){
        //分页构造器对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据name进行like模糊查询
        queryWrapper.like(name != null,Setmeal::getName,name);
        //添加排序条件，根据更新时间降序排列
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealservicejiekou.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = fuwufenleijiekou.getById(categoryId);
            if(category != null){
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);
        return R.success(1,"查询单条成功",dtoPage);
    }
    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<Object> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setmealservicejiekou.removeWithDish(ids);
        return R.success(1,"套餐数据删除成功",null);
    }
    //停售起售接口
    @PostMapping("/status/{num}")
    public  R<Object>  status(@PathVariable Integer num,long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            //修改菜品状态
            setmeal.setStatus(num);
            setmeal.setId(id);
            setmealservicejiekou.updateById(setmeal);
        }
        return R.success(1, "停售起售修改成功", null);
    }
    @GetMapping("/{id}")
    public R<Object> get(@PathVariable Long id){
        SetmealDto dishDto = setmealservicejiekou.getByIdWithDish(id);
        return R.success(1,"查询成功",dishDto);
    }
    @PutMapping("")
    public R<Object> update(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealservicejiekou.updateWithDish(setmealDto);
        return R.success(1,"修改菜品成功",null);
    }
    /**
     * 根据条件查询套餐数据
     */
    @GetMapping("/list")
    public R<Object> list(Setmeal setmeal){
        // 创建匿名查询对象
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealservicejiekou.list(queryWrapper);
        return R.success(1,"套餐查询成功",list);
    }

}
